#!/bin/bash
echo "准备安装依赖"
yum install -y perl-devel zlib-devel pcre pcre-devel gd-devel.x86_64 gcc-c++
echo "依赖安装完成"

echo "===================="




echo "准备下载nginx"
mkdir tmp && cd tmp
wget https://gitee.com/dorne/nginx-support/attach_files/881643/download/nginx-1.18.0.tar.gz && tar -zxvf nginx-1.18.0.tar.gz && rm -f nginx-1.18.0.tar.gz
cd nginx-1.18.0 && mkdir srclib && cd srclib
echo "下载nginx完成"
echo "===================="
sleep 1  # Waits 1 seconds.



echo "准备下载nginx插件"
wget https://gitee.com/dorne/nginx-support/attach_files/881644/download/openssl-1.1.1k.tar.gz && tar -zxvf openssl-1.1.1k.tar.gz && rm -f openssl-1.1.1k.tar.gz
sleep 1  # Waits 1 seconds.

wget https://gitee.com/dorne/nginx-support/attach_files/881637/download/ngx_devel_kit-0.3.1.tar.gz && tar -zxvf ngx_devel_kit-0.3.1.tar.gz && rm -f ngx_devel_kit-0.3.1.tar.gz
sleep 1  # Waits 1 seconds.

wget https://gitee.com/dorne/nginx-support/attach_files/881639/download/lua-nginx-module-0.10.14.tar.gz && tar -zxvf lua-nginx-module-0.10.14.tar.gz && rm -f lua-nginx-module-0.10.14.tar.gz
sleep 1  # Waits 1 seconds.

wget https://gitee.com/dorne/nginx-support/attach_files/881636/download/ngx_cache_purge-2.3.tar.gz && tar -zxvf ngx_cache_purge-2.3.tar.gz && rm -f ngx_cache_purge-2.3.tar.gz
sleep 1  # Waits 1 seconds.

wget https://gitee.com/dorne/nginx-support/attach_files/881638/download/nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz && tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz && rm -f nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz
sleep 1  # Waits 1 seconds.

wget https://gitee.com/dorne/nginx-support/attach_files/881635/download/nginx-http-concat-1.2.2.tar.gz && tar -zxvf nginx-http-concat-1.2.2.tar.gz && rm -f nginx-http-concat-1.2.2.tar.gz
sleep 1  # Waits 1 seconds.

wget https://gitee.com/dorne/nginx-support/attach_files/881642/download/pcre-8.42.tar.gz && tar -zxvf pcre-8.42.tar.gz && rm -f pcre-8.42.tar.gz
sleep 1  # Waits 1 seconds.

wget https://gitee.com/dorne/nginx-support/attach_files/881634/download/ngx_dynamic_upstream-0.1.6.tar.gz && tar -zxvf ngx_dynamic_upstream-0.1.6.tar.gz && rm -f ngx_dynamic_upstream-0.1.6.tar.gz
sleep 1  # Waits 1 seconds.

echo "下载nginx插件完成"
echo "===================="



echo "准备下载luajit2"
cd ../..
wget https://gitee.com/dorne/nginx-support/attach_files/881640/download/luajit2-2.1-20210510.tar.gz && tar -zxvf luajit2-2.1-20210510.tar.gz && rm -f luajit2-2.1-20210510.tar.gz
sleep 1  # Waits 1 seconds.

cd luajit2-2.1-20210510
make
make install PREFIX=/usr/local/luajit
cd ..
echo "下载luajit2完成"
echo "===================="



echo "安装luajit环境变量"
echo "===================="
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
source /etc/profile
ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2



echo "添加www用户"
echo "===================="
adduser www --system --no-create-home
groupadd www --system



cd nginx-1.18.0
./configure --user=www --group=www --prefix=/usr/local/nginx --with-openssl=srclib/openssl-1.1.1k --add-module=srclib/ngx_dynamic_upstream-0.1.6 --add-module=srclib/ngx_devel_kit-0.3.1 --add-module=srclib/lua-nginx-module-0.10.14 --add-module=srclib/ngx_cache_purge-2.3 --add-module=srclib/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 --add-module=srclib/nginx-http-concat-1.2.2 --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-pcre=srclib/pcre-8.42

make -j2
make install
echo "nginx编译完成"
echo "===================="



#生成启动脚本，并加入开机执行
cat>/etc/init.d/nginx<<EOF
#!/bin/bash
#Startup script for the nginx Web Server
#chkconfig: 2345 85 15
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf

#检查程序是否在运行
is_exist() {
  pid=\`ps auxww | grep nginx | grep master | awk '{print \$2}' \`
  #如果不存在返回1，存在返回0
  if [ -z "\$pid" ]; then
    return 1
  else
    return 0
  fi
}

usage() {
        echo "Usage: \$0 [start|status|restart|reload|stop|test|show]"
        exit 1
}

status() {
        ps auxww | grep nginx | grep master
}

test() {
        \$nginx -t -c \$conf
}

stop() {
        is_exist
        if [ \$? -eq "0" ]; then
                \$nginx -s stop
                echo "stop nginx"


                #强制杀进程
                #killall -9 nginx
                #echo " done."
        else
                echo "nginx is not running"
        fi
        
}

start() {
        is_exist
        if [ \$? -eq "0" ]; then
                echo "nginx has run"
        else
                echo "starting nginx"
                \$nginx -c \$conf
        fi
        
}

reload() {
        is_exist
        if [ \$? -eq "0" ]; then
                \$nginx -s reload
                echo "reload done."
        else
                echo "nginx is not running"
        fi
        
}

restart() {
        stop
        sleep 2  # Waits 2 seconds.
        start
        echo "restart done."
}

case \$1 in
        start)
                start
                ;;
        stop)
                stop
                ;;
        test)
                test
                ;;
        reload)
                reload
                ;;
        restart)
                restart
                ;;
        status)
                status
                ;;
        *)
                usage
                ;;
esac
EOF
echo "注册nginx服务完成"
echo "===================="


chmod 755 /etc/init.d/nginx
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
echo "添加开机启动nginx"
echo "===================="

echo "nginx安装完成"
echo "nginx命令如下"
echo "service nginx start"
echo "service nginx status"
echo "service nginx restart"
echo "service nginx reload"
echo "service nginx stop"
echo "service nginx test"
echo "service nginx show"



